package com.lepu.app.widget;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.app.utils.BitmapLoadUtil;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.core.AsyncRequest;
import com.core.lib.ui.auto_viewpager.AutoScrollViewPager;
import com.core.lib.ui.auto_viewpager.AutoScrollViewPagerAdapter;
import com.lepu.app.main.bean.BeanFocus;
import com.lepu.pasm.R;

public class FocusTwoView extends LinearLayout implements AsyncRequest {

	private static final int INDEX_BANNER_SHOP_DEFAULT_COUNT = 2;
	
	private ArrayList<BeanFocus> mFocusList;

	public AutoScrollViewPager mAutoScrollViewPager;
	private AutoScrollViewPagerAdapter mPagerAdapter;
	private LinearLayout mFocusIndicator;
	private TextView mFocusName;

	private int mFocusPageCount;
	private BaseFragmentActivity mBaseFragmentActivity = null;

	public FocusTwoView(Context context, ArrayList<BeanFocus> dataList) {
		super(context);

		mFocusList = dataList;
		mBaseFragmentActivity = (BaseFragmentActivity) context;

		LayoutInflater.from(mBaseFragmentActivity).inflate(R.layout.main_focus_view_two, this, true);
		init();
	}

	public FocusTwoView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	@SuppressWarnings("deprecation")
	private void init() {

		FrameLayout contentLayout = (FrameLayout) findViewById(R.id.contentLayout);

		WindowManager wm = (WindowManager) mBaseFragmentActivity.getSystemService(Context.WINDOW_SERVICE);
		int width = wm.getDefaultDisplay().getWidth();
		LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
		params.width = width;
		params.height = width;
		contentLayout.setLayoutParams(params);

		mAutoScrollViewPager = (AutoScrollViewPager) findViewById(R.id.autoScrollViewPager);
		mFocusIndicator = (LinearLayout) findViewById(R.id.focusIndicator);
		mFocusName = (TextView) findViewById(R.id.focusName);

		int count = 0;
		if (mFocusList != null && mFocusList.size() > 0) {
		    count = mFocusList.size();
		}else{
			count = INDEX_BANNER_SHOP_DEFAULT_COUNT;
		}

		for (int i = 0; i < count; i++) {
			View v = new View(mBaseFragmentActivity);
			v.setBackgroundResource(R.drawable.app_point_two_bg);
			LayoutParams layoutParams = new LayoutParams(15, 15);
			layoutParams.setMargins(5, 5, 5, 5);
			mFocusIndicator.addView(v, layoutParams);
		}

		mPagerAdapter = new AutoScrollViewPagerAdapter(mBaseFragmentActivity) {

			@Override
			public List<View> buildViews() {
				return addView();
			}
		};

		mAutoScrollViewPager.setAdapter(mPagerAdapter);

		mFocusPageCount = count;

		mAutoScrollViewPager.setInterval(2000);
		mAutoScrollViewPager.setOnPageChangeListener(new MyOnPageChangeListener());
		mAutoScrollViewPager.setAutoScrollDurationFactor(10);
		mAutoScrollViewPager.start();
	}

	public void onResumeViewPager() {
		if (mAutoScrollViewPager != null) {
			mAutoScrollViewPager.onResume();
		}
	}

	public void onPauseViewPager() {
		if (mAutoScrollViewPager != null) {
			mAutoScrollViewPager.onPause();
		}
	}

	private List<View> addView() {

		List<View> list = new ArrayList<View>();

		if (mFocusList != null && mFocusList.size() > 0) {
			for (int i = 0; i < mFocusList.size(); i++) {
				final BeanFocus item = mFocusList.get(i);
				ImageView imageView = new ImageView(mBaseFragmentActivity);
				imageView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
				imageView.setScaleType(ImageView.ScaleType.FIT_XY);

				String imageUrl = item.Cover; 
				BitmapLoadUtil.displayImage(R.drawable.default_icon_big,imageUrl,imageView);

				imageView.setOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(View arg0) {

					}
				});

				list.add(imageView);
			}
		}

		return list;
	}

	public class MyOnPageChangeListener implements OnPageChangeListener {

		@Override
		public void onPageSelected(int position) {
			int pos = position % mFocusPageCount;
			for (int i = 0; i < mFocusIndicator.getChildCount(); i++) {
				mFocusIndicator.getChildAt(i).setEnabled(false);
			}

			mFocusIndicator.getChildAt(pos).setEnabled(true);
			mFocusName.setVisibility(View.GONE);

		    mFocusName.setText(mFocusList.get(pos).ACTitle);
		}

		@Override
		public void onPageScrolled(int position, float positionOffset,
				int positionOffsetPixels) {
		}

		@Override
		public void onPageScrollStateChanged(int arg0) {
		}
	}

	@Override
	public void RequestComplete(Object object, Object data) {
	}

	@Override
	public void RequestError(Object object, int errorId, String errorMessage) {
	}
}
